<?php

/*
 * Create by Indonesian Brain Solutions
 * E-mail : cs@mesinit.com
 * Programmer : sony@sonywibisono.com
 */
if (!defined('__BACKEND__'))
    die("you cannot directly access this page");

include("class/AppMenus.php");
include("class/AppMenuModules.php");

_showMenuHeader();

function add(){
    //$modulelist=  select_module(0);
    $granted=  create_radio("grant_admin", "Y;N", "Yes;No","");
    $enabled=  create_radio("enabled", "Y;N", "Yes;No","");
    $modulelist=modules_checkbox("");
    echo "<div id=\"table_data\"><h2>Add Menu</h2>
        <form action=\"?mod=admin&file=menus\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\"># </td></tr>
<tr><td class=\"field_name\">Caption</td><td class=\"field_object\"><input type=\"text\" name=\"caption\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Tool Tips</td><td class=\"field_object\">
<textarea cols=\"40\" rows='5' name=\"tool_tips\" ></textarea> </td></tr>
<tr><td class=\"field_name\">Grant Admin</td><td class=\"field_object\">$granted</td></tr>
<tr><td class=\"field_name\">Enabled</td><td class=\"field_object\">$enabled </td></tr>
<tr><td class=\"field_name\">Icon Url</td><td class=\"field_object\"><input type=\"text\" name=\"icon_url\"  class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Url Link</td><td class=\"field_object\">
<input type=\"text\" name=\"menu_url\"  class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Show in Module </td>
<td class=\"field_object\">$modulelist </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Add New\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form></div>";
}
function edit(){
    global $db,$id;
    if (!empty($id)){
        $mods=  get_menu_modules($id);
    $sql="select * from app_menus where id=$id";
    $row=$db->fetch_row($sql);
    extract($row);
    //$modulelist=  select_module($module_id);
    $modulelist=modules_checkbox($modules);
    $granted=  create_radio("grant_admin", "Y;N", "Yes;No",$grant_admin);
    $enables=  create_radio("enabled", "Y;N", "Yes;No",$enabled);
    echo "<div id=\"table_data\"><h2>Edit Menu</h2>
        <form action=\"?mod=admin&file=menus\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
    <input type=\"hidden\" name=\"id\" value=\"$id\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\"># $id</td></tr>
<tr><td class=\"field_name\">Caption</td><td class=\"field_object\">
<input type=\"text\" name=\"caption\" class=\"textbox\" value=\"$caption\"/> </td></tr>
<tr><td class=\"field_name\">Tool Tips</td><td class=\"field_object\">
<textarea cols=\"40\" rows='5' name=\"tool_tips\" >$tool_tips</textarea> </td></tr>
<tr><td class=\"field_name\">Grant Admin</td><td class=\"field_object\">$granted</td></tr>
<tr><td class=\"field_name\">Enabled</td><td class=\"field_object\">$enables </td></tr>
<tr><td class=\"field_name\">Icon Url</td><td class=\"field_object\">
<input type=\"text\" name=\"icon_url\"  class=\"textbox\" value=\"$icon_url\"/> </td></tr>
<tr><td class=\"field_name\">Filename</td><td class=\"field_object\">
<input type=\"text\" name=\"menu_url\"  class=\"textbox\" value=\"$menu_url\"/> </td></tr>
<tr><td class=\"field_name\">Show in Module </td>
<td class=\"field_object\">$modulelist </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Update\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form></div>";
    }
}
function delete_menu($menu_id){
    global $db;
    $sql="delete from app_menu_modules where app_menu_id=$menu_id";
    $db->query($sql);
}
function add_process(){
    global $id,$caption,$tool_tips,$grant_admin,$enabled,$icon_url,$menu_url,$module_id,$mod,$file,$modules;
    if (!empty($caption)){
    //echo "Add Process Record";
        if (is_array($modules)){
            $mods=  implode(";", $modules);
        } else {
            $mods=$modules;
        }
    $app_menus=new AppMenus($id,$caption,$tool_tips,$grant_admin,$enabled,$icon_url,$menu_url,$mods);
    $id=$app_menus->insert();
    for($i=0;$i<count($modules);$i++){
    $menu_mods=new AppMenuModules(0,$id,$modules[$i],'Y');
    $menu_mods->automatic_update();
     
    
    }
    $url="?mod=$mod&file=$file";
    $msg="Add New Record Success";
    refresh_to($url, $msg, 2);
    }
}
function edit_process(){
    global $id,$caption,$tool_tips,$grant_admin,$enabled,$icon_url,$menu_url,$module_id,$mod,$file,$modules;
    if (!empty($caption)){
    //echo "Add Process Record";
        if (is_array($modules)){
            $mods=  implode(";", $modules);
        } else {
            $mods=$modules;
        }
    $app_menus=new AppMenus($id,$caption,$tool_tips,$grant_admin,$enabled,$icon_url,$menu_url,$mods);
    $app_menus->update();
    delete_menu($id);
    for($i=0;$i<count($modules);$i++){
    $menu_mods=new AppMenuModules(0,$id,$modules[$i],'Y');
    $menu_mods->automatic_update();
     
    
    }
    $url="?mod=$mod&file=$file";
    $msg="Update Record Success";
    refresh_to($url, $msg, 2);
    }
}
function delete_process(){
    global $db,$id,$mod,$file;
    if (!empty($id)){
    echo "Delete Process Record";
    
    $sql="delete from app_menus where id=$id";
    $db->query($sql);
    $url="?mod=$mod&file=$file";
    $msg="Delete Record Success";
    refresh_to($url, $msg, 2);
    }
}
function __default(){
    global $db,$page,$sortby,$sortorder,$mod,$pagesize,$file;
    if (empty($page)){
        $page=0;
        $start=0;
        $sql="select a.* from app_menus a order by caption";
    } else {
        $start=$page*$pagesize;
        $sql="select a.* from app_menus a
            order by caption limit $start,$pagesize";
    }
   
    
    $query=$db->query($sql);
    echo "<h2>Application Menu List</h2>
        <div id=\"caption_table\"><a class='action_link' href=\"?mod=$mod&file=menus&action=add\">Add New</a></div>";
    echo "<div id=\"table_data\"><table width=\"100%\" cellpadding=2 cellspacing=0>
        <tr><th class='table_left_top'>Id</th><th>Caption</th><th>Tool Tips</th><th>Grant Admin</th>
        <th>Enabled</th><th>Icon Url</th><th>Filename</th><th>Module</th><th class='table_right_top'>Action</th></tr>";
    $i=0;
    while ($row=$db->fetch_array($query)){
        extract($row);
        if ($i%2==0){
            $class="row_even";
        } else {
            $class="row_odd";
        }
        echo "<tr class=\"$class\"><td>$id</td><td>$caption</td><td>$tool_tips</td>
            <td align=center>$grant_admin</td>
            <td align=center>$enabled</td><td>$icon_url</td><td>$menu_url</td><td>$modules</td>
                <td align=center><a href=\"?mod=$mod&file=$file&action=edit&id=$id\" class=\"icon_link\">
                    <img class='icon_small' src='images/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\" class=\"icon_link\">
            <img class='icon_small' src='images/delete.png' title='Delete'/></a></td></tr>";
    
        $i++;
    }
    echo "<tr class='row_odd'><td class=\"table_left_bottom\"></td>
        <td colspan='5'>&nbsp;</td><td class=\"table_right_bottom\"></td></tr></table>";
    
        $np=$page+1;
        $pp=$page-1;
        echo "<a href=\"?mod=$mod&file=$file&page=$pp\">Previous Page</a> | ";
        echo "<a href=\"?mod=$mod&file=$file&page=$np\">Next Page</a>";
    
    echo "</div>";
}
global $action,$user_id,$login_status,$is_admin;
switch($action){
    case 'add':
        add();
        break;
    case 'edit':
        edit();
        break;
    case 'add_process':
        add_process();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete_process();
        break;
    default:
        __default();
}
?>